﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Text;
using Assets.Scripts.TDuResource;

namespace Assets.Scripts.TDuResource
{
    class TDuSceneCfg
    {
        //配置相关存储变量;
        public SceneAttr sceneAttr;
        public List<string> vecCaption;
        public Property sceneProperty;
        public List<string> vecSound;
        public List<SceneSteps> vecOperSteps;
        public List<BipAnimation> vecFindSkeleton;
        public List<ToolsTranslate> vecToolTranslates;
        public List<string> setBillboards;
        public List<string> setReflections;
        public List<SceneSprite> vecSceneSprites;
        public ProjectConfig? sceneProject;
        public List<ViewPort> vecViewPorts;
        public List<SceneNode> vecSceneNodes;
        public SceneEnvironment sceneEnvironment;
        public SceneSwitch sceneSwitch;
        public List<SchematicView> vecSchematic;
        public List<ExplosiveView> vecExplosive;
        public FullInitVarState sceneInitState;
        public Dictionary<string, VariableNode> vecVariableNode;
        public Dictionary<string, VariableNode> mapVariableNodes;
        public List<VariableNode> arrUI;
        public List<VariableState> arrVariableStates;
        public Dictionary<string, VariableNode> mapSteps;

        //配置解析器;
        private XmlDocument sceneCfgDocument;

        public TDuSceneCfg()
        {
            vecCaption = new List<string>();
            vecSound = new List<string>();
            vecOperSteps = new List<SceneSteps>();
            vecFindSkeleton = new List<BipAnimation>();
            vecToolTranslates = new List<ToolsTranslate>();
            setBillboards = new List<string>();
            setReflections = new List<string>();
            vecSceneSprites = new List<SceneSprite>();
            vecViewPorts = new List<ViewPort>();
            vecSceneNodes = new List<SceneNode>();
            vecSchematic = new List<SchematicView>();
            vecExplosive = new List<ExplosiveView>();

            vecVariableNode = new Dictionary<string, VariableNode>();
            mapVariableNodes = new Dictionary<string, VariableNode>();
            arrUI = new List<VariableNode>();
            mapSteps = new Dictionary<string, VariableNode>();
            arrVariableStates = new List<VariableState>();

            sceneCfgDocument = new XmlDocument();

            #region sceneAttr初始化
            {
                sceneAttr.nTimes = 0;
                sceneAttr.nFPS = 0;
                sceneAttr.nLimitErrorCount = 3;
                sceneAttr.fScores = 0;
                sceneAttr.bLoopByName = false;
                sceneAttr.strNavagation = "";
                sceneAttr.strBgSound = "";
            }
            #endregion

            #region sceneProperty初始化
            {
                sceneProperty.strNavagation = "";
                sceneProperty.strAssistPic = "";
                sceneProperty.strInfoCap = "";
                sceneProperty.strSkyBox = "";
                sceneProperty.fAniSpeed = 0;
                sceneProperty.fWalkSpeed = 0;
                sceneProperty.arrBackGround = new float[4];
                //sceneProperty.strToolPanel = "";
                //sceneProperty.CameraMinZ = 0;
                sceneProperty.transparentObj = "";

                sceneProperty.bAnimationState = false;
                sceneProperty.bMultiViews = false;
                sceneProperty.bCaption = false;
                sceneProperty.bSound = false;
                sceneProperty.bBgColor = false;
                sceneProperty.bShowVP = false;
                sceneProperty.bFindView = false;
                sceneProperty.bHavePic = false;
                sceneProperty.bAllowThrow = false;
                sceneProperty.bHaveRota = false;
                sceneProperty.bToolAutoBack = false;

                sceneProperty.nHintPos = 0;
                sceneProperty.nBigPos = 0;
                sceneProperty.nSmallPos = 0;
                sceneProperty.arrObjRGBA = new int[4];
                sceneProperty.ePracticeMode = PracticeMode.E_PRACTICE;
                sceneProperty.eSceneType = SceneType.E_SIMULATION;
            }
            #endregion

            #region sceneProject初始化
            {
                sceneProject = null;
            }
            #endregion

            #region sceneEnvironment初始化
            {
                sceneEnvironment.strEnvTexture = "";
                sceneEnvironment.strSkyTexture = "";
                sceneEnvironment.bUseGround = true;
                sceneEnvironment.bUseSkybox = true;
                sceneEnvironment.bUse = false;
                sceneEnvironment.arrGroundColor = new double[3];
            }
            #endregion

            #region sceneSwitch初始化
            {
                sceneSwitch.nChoice = 0;
                sceneSwitch.arrCases = new List<SwitchCase>();
                //sceneSwitch.arrCases.strCaseName = "";
                //sceneSwitch.arrNodeName = new List<string>();
            }
            #endregion

            #region sceneInitState初始化
            {
                sceneInitState.strDescription = "";
                sceneInitState.strSubject = "";
                sceneInitState.arrVarStates = new List<InitVarState>();
            }
            #endregion
        }

        public bool loadSceneCfg(string strCfgUrl)
        {
            Debug.Log("[Output]-------Scene Cfg Url:" + strCfgUrl);

            if (strCfgUrl.Length <= 0)
                return false;

            WWW www = new WWW(strCfgUrl);

            //加载完毕后跳出循环，否则等待加载
            while (!www.isDone)
            {
            }

            sceneCfgDocument.LoadXml(www.text);
            
            return true;
        }

        public bool readSceneCfg()
        {
            TDuSceneCfgReader reader = new TDuSceneCfgReader();

            sceneAttr = reader.readScene(sceneCfgDocument);
            vecCaption = reader.readSubTitle(sceneCfgDocument);
            sceneProperty = reader.readProperty(sceneCfgDocument);
            sceneProject = reader.readProject(sceneCfgDocument);
            vecSound = reader.readSound(sceneCfgDocument);
            vecOperSteps = reader.readOperStep(sceneCfgDocument);
            vecFindSkeleton = reader.readBipAnimation(sceneCfgDocument);
            vecToolTranslates = reader.readToolsTranslate(sceneCfgDocument);
            setBillboards = reader.readBillboards(sceneCfgDocument);
            setReflections = reader.readReflections(sceneCfgDocument);
            vecSceneSprites = reader.readSceneSprites(sceneCfgDocument);
            vecViewPorts = reader.readViewports(sceneCfgDocument);
            vecSceneNodes = reader.readSceneNodes(sceneCfgDocument);
            sceneSwitch = reader.readSceneSiwtch(sceneCfgDocument);
            vecSchematic = reader.readSchematicView(sceneCfgDocument);
            vecExplosive = reader.readExplosiveView(sceneCfgDocument);
            sceneInitState = reader.readInitState(sceneCfgDocument);

            reader.readVariableNodes(sceneCfgDocument, ref vecVariableNode, mapVariableNodes, arrUI, mapSteps);
            reader.readVarialbeStates(sceneCfgDocument, arrVariableStates);

            return true;
        }

        public bool writeSceneCfg()
        {

            return true;
        }
    }
}
